package jsyf.com.util;

import java.util.HashSet;
import java.util.Set;

import jsyf.com.constant.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author: @韩武洽 @Wyshown
 * @Version: V1.0.0
 */
@Configuration
// 启动缓存配置
@ComponentScan("jsyf")
@PropertySource("classpath:redis.properties")
public class ClusterConfig {

	@Autowired
	private Environment environment;
	

	/**
	 * @param
	* @Description: 方法描述,必填  <br/>
	* @return: redis.clients.jedis.JedisCluster
	* @throw:
	 * @author: @韩武洽 @Wyshown
	 * @Version: V1.0.0
	*/
    @Bean
	public JedisCluster getRedisCluster(){
	    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();

	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes1"), Integer.parseInt(environment.getProperty("redis.cluster.port1"))));
	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes2"), Integer.parseInt(environment.getProperty("redis.cluster.port2"))));
	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes3"), Integer.parseInt(environment.getProperty("redis.cluster.port3"))));
	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes4"), Integer.parseInt(environment.getProperty("redis.cluster.port4"))));
	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes5"), Integer.parseInt(environment.getProperty("redis.cluster.port5"))));
	    jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes6"), Integer.parseInt(environment.getProperty("redis.cluster.port6"))));

	    JedisPoolConfig cfg = new JedisPoolConfig();
	    cfg.setMaxTotal(Integer.parseInt(environment.getProperty("redis.cluster.config.max-total")));
	    cfg.setMaxIdle(Integer.parseInt(environment.getProperty("redis.cluster.config.max-idle")));
	    cfg.setMaxWaitMillis(Integer.parseInt(environment.getProperty("redis.cluster.config.max-waitmillis")));
	    cfg.setTestOnBorrow(Boolean.parseBoolean(environment.getProperty("redis.cluster.config.onborrow")));

	    JedisCluster jc = new JedisCluster(jedisClusterNode, Integer.parseInt(environment.getProperty("redis.cluster.timeout")),
				Integer.parseInt(environment.getProperty("redis.cluster.max-redirections")),Integer.parseInt(environment.getProperty("redis.cluster.config.max-attempts"))
				,String.valueOf(environment.getProperty("redis.cluster.password")), cfg);
	    //初始化ios和Android的图片验证码同行证
		jc.set(Const.IOS_IMAGE_CODE_KEY, Const.IOS_IMAGE_CODE_VALUE);
		jc.set(Const.ANDROID_IMAGE_CODE_KEY, Const.ANDROID_IMAGE_CODE_VALUE);
		return jc;
	}
	
}
